# -*- coding: utf-8 -*-
"""
Created on Tue Mar 27 09:59:41 2018

@author: mojm
"""
import numpy as np
import sklearn.datasets
import matplotlib.pyplot as plt
import sklearn.linear_model
import sklearn.metrics

#def plot_decision_boundary(pred_func):  
#    # 设定最大最小值，附加一点点边缘填充  
#    x_min, x_max = X2[:, 0].min() - .5, X2[:, 0].max() + .5  
#    y_min, y_max = X2[:, 1].min() - .5, X2[:, 1].max() + .5  
#    h = 0.01  
#  
#    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))  
#  
#    # 用预测函数预测一下  
#    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])  
#    Z = Z.reshape(xx.shape)  
#  
#    # 然后画出图  
#    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)  
#    plt.scatter(X2[:, 0], X2[:, 1], c=y, cmap=plt.cm.Spectral) 

n_samples=5000

np.random.seed(0)
X, y = sklearn.datasets.make_moons(10, noise=0.20)
X2, y2, coef = sklearn.datasets.make_regression(n_samples, n_features=20, noise=10, coef=True)


sampleRatio = 0.9
targetRatio = round(1-sampleRatio, 2)
sampleBoundary = int(n_samples*sampleRatio)

shuffleIdx = range(n_samples)
np.random.shuffle(shuffleIdx)

train_features = X2[shuffleIdx[:sampleBoundary]]
train_targets = y2[shuffleIdx[:sampleBoundary]]
#plt.scatter(range(sampleBoundary), train_targets,  color='black')

test_features = X2[shuffleIdx[sampleBoundary:]]
test_targets = y2[shuffleIdx[sampleBoundary:]]


print('original coef is ... ' + str(coef))
#plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)

#plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)

### train
linearRegression = sklearn.linear_model.LinearRegression()
linearRegression.fit(train_features,train_targets)

ridgeRegression = sklearn.linear_model.RidgeCV(alphas=[0.15,0.5,0.7,20])
ridgeRegression.fit(train_features, train_targets)
print ('alpha(Ridge) is ... ' + str(ridgeRegression.alpha_))

lassoRegression = sklearn.linear_model.LassoCV(alphas = [0.15,0.5,0.7,20])
lassoRegression.fit(train_features, train_targets)
print ('alpha(Lasso) is ... ' + str(lassoRegression.alpha_))



### predict
predict_targets = linearRegression.predict(test_features)
predict_targets_Ridge = ridgeRegression.predict(test_features)
predict_targets_Lasso = lassoRegression.predict(test_features)

#predict_y = linearRegression.predict(3.123932)
print ('predict coef is ... ' + str(linearRegression.coef_))
print ('predict coef(Ridge) is ...' + str(ridgeRegression.coef_))
print ('predict coef(Lasso) is ...' + str(lassoRegression.coef_))
print ('predict intercept is ... ' + str(linearRegression.intercept_))
#print ('predict y is ... ' + str(predict_y))

#clf = sklearn.linear_model.LogisticRegressionCV()
#clf.fit(X2, y2)

### draw
#plt.plot(train_features, train_features*coef, color='blue', linewidth=3)
plt.scatter(range(int(n_samples*targetRatio)), test_targets, color='green')
plt.scatter(range(int(n_samples*targetRatio)), predict_targets, color='red')
#plt.scatter(range(int(n_samples*targetRatio)), predict_targets_Ridge, color='yellow')
#plt.plot(range(5),linestyle='--', linewidth=3,color='purple')
#plt.ylabel('Numbers from 1-5')
#plt.xlabel('Love Yohanna')

# Plot the decision boundary
#plot_decision_boundary(lambda x: clf.predict(x))
plt.title("Linear Regression")
plt.show()

### verify
print "MSE:", sklearn.metrics.mean_squared_error(test_targets, predict_targets)
print "MSE(Ridge):", sklearn.metrics.mean_squared_error(test_targets, predict_targets_Ridge)
print "MSE(Lasso):", sklearn.metrics.mean_squared_error(test_targets, predict_targets_Lasso)
#accuracy = sklearn.metrics.accuracy_score(test_targets, predict_targets)
#print accuracy


